'''
Created on 10/ott/2011

@author: Ivan
'''
import wx  
import wx.lib.ogl as ogl  
from wx._windows import StatusBar

      
class PannelloER ( wx.Panel ):
    
        def __init__( self, parent ):
            wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, style = wx.TAB_TRAVERSAL )
            
            
            agg_q = wx.Button(self, -1, 'agg. quadrato')  
            agg_c = wx.Button(self, -1, 'agg. cerchio')  
            zoom_in = wx.Button(self, -1,'zoom in')
            zoom_out = wx.Button(self, -1,'zoom out')
            canc = wx.Button(self, -1, 'cancella')
            self.frame= parent
            self.frame.SetStatusText("prova")
            
            # ------ la parte importante:  
            # ogni canvas ha un diagramma, devono essere collegati tra loro,   
            # e si usano entrambi a seconda delle occasioni...  
            self.canvas = ogl.ShapeCanvas(self)
            self.diag = ogl.Diagram()  
            self.diag.SetCanvas(self.canvas)  
            self.canvas.SetDiagram(self.diag)  
            self.canvas.SetBackgroundColour("White")
            y=self.canvas.GetMaxHeight()
            self.MaxH=200
            self.MaxW=1000
            
            
            self.canvas.SetScrollbars(20, 20, self.MaxW/20, self.MaxH/20)
            

            self.canvas.SetScrollbar(30,30,60,60)
            # -------  
            s = wx.BoxSizer(wx.VERTICAL)  
            for b in (agg_q, agg_c,zoom_in,zoom_out, canc):  
                s.Add(b, 0, wx.EXPAND|wx.ALL, 5)  
            s1 = wx.BoxSizer()  
            s1.Add(s, 0, wx.EXPAND, 0)  
            s1.Add(self.canvas, 1, wx.EXPAND|wx.ALL, 5)  
            self.SetSizer(s1)  
              
            self.Bind(wx.EVT_BUTTON, self.su_agg_q, agg_q)  
            self.Bind(wx.EVT_BUTTON, self.su_agg_c, agg_c)  
            self.Bind(wx.EVT_BUTTON, self.zoom_in, zoom_in) 
            self.Bind(wx.EVT_BUTTON, self.zoom_out, zoom_out)
            self.Bind(wx.EVT_BUTTON, self.su_canc, canc)  
      
        def su_agg_q(self, evt): # aggiunge un quadrato  
            q = ogl.RectangleShape(50.0, 50.0)  
            self.aggiungi_forma(q)  
          
        def su_agg_c(self, evt):  # aggiunge un cerchio  
            c = ogl.CircleShape(50.0)  
            self.aggiungi_forma(c) 
            
        def zoom_in(self,evt):
            
            self.canvas.SetScale(self.canvas.GetScaleX()+0.5,self.canvas.GetScaleY()+0.5)
            self.resizeScrool(self.MaxH/0.5, self.MaxW/0.5)
            self.canvas.Refresh()
        def zoom_out(self,evt):
            
            self.canvas.SetScale(self.canvas.GetScaleX()-0.5,self.canvas.GetScaleY()-0.5)
            self.resizeScrool(self.MaxH*0.5, self.MaxW*0.5)
            self.canvas.Refresh()
              
        def su_canc(self, evt):  # se qualcosa e' selezionato, lo elimina  
            for shape in self.diag.GetShapeList():  
                if shape.Selected():   
                    shape.Select(False)  
                    self.canvas.RemoveShape(shape)  
            self.canvas.Refresh(False)  
          
        def aggiungi_forma(self, forma):  
            # una funzione di convenienza, solo per non ripetere due volte  
            # questo codice (in su_agg_q e su_agg_c)  
            forma.SetX(60.0)  
            forma.SetY(60.0)  
            self.canvas.AddShape(forma)  
            # ---- collego la forma appena creata al gestore degli eventi  
            # in questo modo  
            evthandler = MyEvtHandler(self.frame,self)  
            evthandler.SetShape(forma)  
            evthandler.SetPreviousHandler(forma.GetEventHandler())  
            forma.SetEventHandler(evthandler)  
            # ------  
            forma.Show(True)  
            self.canvas.Refresh(False) 
        def setMaxHeight(self,h):
            self.MaxH=h
        def setMaxWidth(self,w):
            self.MaxW=w
        def resizeScrool(self,h,w):
            self.setMaxHeight(h)
            self.setMaxWidth(w)
            self.canvas.SetScrollbars(20, 20, self.MaxW/20, self.MaxH/20)

class MyEvtHandler(ogl.ShapeEvtHandler):  
        # il gestore degli eventi per le forme che creo  
        def __init__(self,parent,panel):  
            ogl.ShapeEvtHandler.__init__(self)  
            self.parent = parent
            self.panel = panel
      
        def OnLeftClick(self, x, y, keys=0, attachment=0):  
            shape = self.GetShape()  
            canvas = shape.GetCanvas()  
            diag = canvas.GetDiagram()  
            # seleziono / deseleziono la forma  
            if shape.Selected():  
                shape.Select(False)  
            else:  
                # deseleziono qualsiasi altra cosa...   
                for s in diag.GetShapeList():   
                    s.Select(False)  
                # ...e seleziono la forma cliccata  
                shape.Select(True)  
            canvas.Refresh(False)  
      
        def OnRightClick(self, x, y, keys=0, attachment=0):  
            shape = self.GetShape()  
            canvas = shape.GetCanvas()  
            # alterno tra bianco e rosso  
            col = shape.GetBrush().GetColour()  
            if col == wx.NamedColour('WHITE'):  
                shape.SetBrush(wx.Brush('RED'))  
            else:  
                shape.SetBrush(wx.Brush('WHITE'))  
            canvas.Refresh(False)   
            
        def OnMovePost(self, dc, x, y, old_x, old_y, display = True):
            shape = self.GetShape()  
            if ( shape.GetX() < 0 ):
                shape.SetX(0)
            if ( shape.GetY() < 0 ):
                shape.SetY(0)
            if ( shape.GetX() > self.panel.MaxW-20 ):
                self.panel.setMaxWidth(shape.GetX()+20)
            if ( shape.GetY() > self.panel.MaxH -20 ):
                self.panel.setMaxHeight(shape.GetY()+20)
            canvas = shape.GetCanvas() 
            x, y = shape.GetX(), shape.GetY()
            
            canvas.SetScrollbars(30, 30, self.panel.MaxW/30, self.panel.MaxH/30,x/30,y/30)
            width, height = shape.GetBoundingBoxMax()
            self.parent.SetStatusText("Pos: (%d, %d)  Size: (%d, %d)" %  (x, y, width, height))
            return ogl.ShapeEvtHandler.OnMovePost(self, dc, x, y, old_x, old_y, display = True)
            
class MyFrame4 (wx.Frame):
    
    def __init__(self, *a, **k):  
        wx.Frame.__init__(self, *a, **k)  
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        self.statusBar = self.CreateStatusBar()
        self.SetStatusText("ciao mondo")
        p = PannelloER(self)  
        
        
    def __del__( self ):
        pass
    


          
if __name__ == '__main__':  
        app = wx.App(False)  
        ogl.OGLInitialize() # <---- MAI DIMENTICARE QUESTO!!  
        MyFrame4(None, -1, 'OGL demo').Show()
        app.MainLoop()  